// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// s [author] rhyme_lph
// f [email]  rhymelph@gmail.com
// Q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/mixin/check_plan_mixin.dart';
import 'package:finance_app/src/provider/dashboard/products_services/views/products_provider.dart';
import 'package:finance_app/src/provider/dashboard/products_services/views/services_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class ProductsAndServicesProvider extends BaseProvider with CheckPlanMixin {
  List<String> menuList = [
    'Import',
    'Deactivate',
    'Delete',
    // 'View Deactivated',
    'Adjust Inventory',
  ];

  bool isService = false;
  bool buttonBarIsUp = true;

  bool usePurchasePrice = false;

  void handleScrollUpdate(ScrollController controller) {
    if (controller.position.userScrollDirection == ScrollDirection.reverse && buttonBarIsUp == true) {
      //向下滑动
      buttonBarIsUp = false;
      update();
    }
    if (controller.position.userScrollDirection == ScrollDirection.forward && buttonBarIsUp == false) {
      buttonBarIsUp = true;
      update();
    }
  }

  @override
  Future get dataFuture => null;

  List<String> get categories {
    if (isService) {
      final service = getChildProvider<ServicesProvider>();
      return service.filterSet.toList();
    } else {
      final product = getChildProvider<ProductsProvider>();
      return product.filterSet.toList();
    }
  }

  @override
  List<BaseProvider> registerChildProviders() {
    return [
      ProductsProvider(),
      ServicesProvider(),
    ];
  }

  void onSelectMenu(String value) async {
    switch (value) {
      case "Import":
        Routes.navigateTo(RouterGenProviders.productAndServiceDataImportPage);
        break;
      case 'Deactivate':
        final result = await Routes.navigateTo(RouterGenProviders.batchProductsAndServicesPage, params: {'isService': isService, 'type': 'Deactivate'});
        if (result != null) {
          if (isService) {
            getChildProvider<ServicesProvider>().refreshController.requestRefresh();
          } else {
            getChildProvider<ProductsProvider>().refreshController.requestRefresh();
          }
        }
        break;
      case 'Delete':
        final result = await Routes.navigateTo(RouterGenProviders.batchProductsAndServicesPage, params: {
          'isService': isService,
          'type': 'Delete',
          'status': isService ? getChildProvider<ServicesProvider>().currentSelectActive : getChildProvider<ProductsProvider>().currentSelectActive
        });
        if (result != null) {
          if (isService) {
            getChildProvider<ServicesProvider>().refreshController.requestRefresh();
          } else {
            getChildProvider<ProductsProvider>().refreshController.requestRefresh();
          }
        }
        break;
      case 'Activate':
        final result = await Routes.navigateTo(RouterGenProviders.batchProductsAndServicesPage, params: {
          'isService': isService,
          'type': 'Activate',
        });
        if (result != null) {
          if (isService) {
            getChildProvider<ServicesProvider>().refreshController.requestRefresh();
          } else {
            getChildProvider<ProductsProvider>().refreshController.requestRefresh();
          }
        }
        break;
      case 'Adjust Inventory':
        final result = await Routes.navigateTo(RouterGenProviders.adjustInventoryListPage, params: {
          'status': isService ? getChildProvider<ServicesProvider>().currentSelectActive : getChildProvider<ProductsProvider>().currentSelectActive
        });
        // Routes.navigateTo(RouterGenProviders.adjustInventoryPage);
        if (result != null) {
          if (isService) {
            getChildProvider<ServicesProvider>().refreshController.requestRefresh();
          } else {
            getChildProvider<ProductsProvider>().refreshController.requestRefresh();
          }
        }
        break;
    }
  }

  int currentIndex = 0;

  TabController tabController;

  void onChange(TabController tabController) {
    currentIndex = (tabController.index + tabController.offset).round();
    if (currentIndex == 0) {
      // if (menuList.length != 4) {
      isService = false;
      getChildProvider<ProductsProvider>().onChangeMenuList();
      // }
    } else if (currentIndex == 1) {
      // if (menuList.length != 2) {
      isService = true;
      getChildProvider<ServicesProvider>().onChangeMenuList();
      // }
    }
  }

  void onNavAdd() {
    if (isPlanExpired()) return;
    if (tabController != null) {
      currentIndex = (tabController.index + tabController.offset).round().clamp(0, 100);
    }
    isService = currentIndex == 1;
    if (isService) {
      Routes.navigateTo(RouterGenProviders.newServicePage, params: {
        'isPro': true,
      });
    } else {
      Routes.navigateTo(RouterGenProviders.newProductPage, params: {
        'isPro': true,
      });
    }
  }
}
